package cn.edu.qzuie.artist.module.td.controller.admin.participateactivitiy;

import org.springframework.web.bind.annotation.*;

import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;


import java.util.*;
import java.io.IOException;

import cn.edu.qzuie.artist.framework.common.pojo.PageParam;
import cn.edu.qzuie.artist.framework.common.pojo.PageResult;
import cn.edu.qzuie.artist.framework.common.pojo.CommonResult;
import cn.edu.qzuie.artist.framework.common.util.object.BeanUtils;
import static cn.edu.qzuie.artist.framework.common.pojo.CommonResult.success;

import cn.edu.qzuie.artist.framework.excel.core.util.ExcelUtils;

import cn.edu.qzuie.artist.framework.apilog.core.annotation.ApiAccessLog;
import static cn.edu.qzuie.artist.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.edu.qzuie.artist.module.td.controller.admin.participateactivitiy.vo.*;
import cn.edu.qzuie.artist.module.td.dal.dataobject.participateactivitiy.ParticipateActivitiyDO;
import cn.edu.qzuie.artist.module.td.service.participateactivitiy.ParticipateActivitiyService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Tag(name = "管理后台 - TD参加活动")
@RestController
@RequestMapping("/td/participate-activitiy")
@Validated
public class ParticipateActivitiyController {

    @Resource
    private ParticipateActivitiyService participateActivitiyService;

    @PostMapping("/create")
    @Operation(summary = "创建参加活动")
    @PreAuthorize("@ss.hasPermission('td:participate-activitiy:create')")
    public CommonResult<Long> createParticipateActivitiy(@Valid @RequestBody ParticipateActivitiySaveReqVO createReqVO) {
        return success(participateActivitiyService.createParticipateActivitiy(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新参加活动")
    @PreAuthorize("@ss.hasPermission('td:participate-activitiy:update')")
    public CommonResult<Boolean> updateParticipateActivitiy(@Valid @RequestBody ParticipateActivitiySaveReqVO updateReqVO) {
        participateActivitiyService.updateParticipateActivitiy(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除参加活动")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('td:participate-activitiy:delete')")
    public CommonResult<Boolean> deleteParticipateActivitiy(@RequestParam("id") Long id) {
        participateActivitiyService.deleteParticipateActivitiy(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得参加活动")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('td:participate-activitiy:query')")
    public CommonResult<ParticipateActivitiyRespVO> getParticipateActivitiy(@RequestParam("id") Long id) {
        ParticipateActivitiyDO participateActivitiy = participateActivitiyService.getParticipateActivitiy(id);
        return success(BeanUtils.toBean(participateActivitiy, ParticipateActivitiyRespVO.class));
    }

//    @GetMapping("/page")
//    @Operation(summary = "获得参加活动分页")
//    @PreAuthorize("@ss.hasPermission('td:participate-activitiy:query')")
//    public CommonResult<PageResult<ParticipateActivitiyRespVO>> getParticipateActivitiyPage(@Valid ParticipateActivitiyPageReqVO pageReqVO) {
//        PageResult<ParticipateActivitiyDO> pageResult = participateActivitiyService.getParticipateActivitiyPage(pageReqVO);
//        return success(BeanUtils.toBean(pageResult, ParticipateActivitiyRespVO.class));
//    }
    @GetMapping("/page")
    @Operation(summary = "获得参加活动分页")
    @PreAuthorize("@ss.hasPermission('td:participate-activitiy:query')")
    public CommonResult<PageResult<ParticipateActivitiyPagesRespVO>> getParticipateActivitiyPage(@Valid ParticipateActivitiyPageReqVO pageReqVO) {
        PageResult<ParticipateActivitiyPagesRespVO> pageResult = participateActivitiyService.getParticipateActivitiyPageWithName(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ParticipateActivitiyPagesRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出参加活动 Excel")
    @PreAuthorize("@ss.hasPermission('td:participate-activitiy:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportParticipateActivitiyExcel(@Valid ParticipateActivitiyPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ParticipateActivitiyDO> list = participateActivitiyService.getParticipateActivitiyPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "参加活动.xls", "数据", ParticipateActivitiyRespVO.class,
                        BeanUtils.toBean(list, ParticipateActivitiyRespVO.class));
    }

}